package com.jnctra.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jnctra.entity.News;
import com.jnctra.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;

@RestController
public class NewsController {
    @Autowired
    private NewsService newsService;

    /**
     * 测试热部署：目前无效，又出问题了？
     * @return 返回字符串
     */
    @GetMapping("/hello")
    public String hello() {
        return "hello, world4";
    }

    /**
     * 查询全部新闻
     * @return 新闻列表
     */
    @GetMapping("/news")
    public List<News> list() {
        return newsService.list();
    }

    /**
     * 分页查询新闻
     * @param current 当前页
     * @param size 每页显示记录数
     * @return 分页数据
     */
    @GetMapping("/news/{current}/{size}")
    public IPage<News> selectPage(@PathVariable("current") long current, @PathVariable("size") long size) {
        IPage<News> iPage = new Page<>(current, size);
        IPage<News> page = newsService.page(iPage);
        return page;
    }

    /**
     * 根据编号选择一条新闻
     * @param newsId
     * @return
     */
    @GetMapping("/news/{newsId}")
    public News getById(@PathVariable("newsId") long newsId) {
        return newsService.getById(newsId);
    }

    /**
     * 添加新闻
     * @param news
     * @return
     */
    @PostMapping("/news")
    public boolean save(@RequestBody News news) {
        return newsService.save(news);
    }

    /**
     * 删除新闻
     * @param news
     * @return
     */
    @DeleteMapping("/news")
    public boolean remove1(@RequestBody News news) {
        return newsService.removeById(news.getNewsId());
    }

    /**
     * 逻辑删除新闻
     * @param news
     * @return
     */
    @PutMapping("/news2")
    public boolean remove2(@RequestBody News news) {
        return newsService.removeById(news.getNewsId());
    }

    /**
     * 批量删除新闻
     * @param newsIds
     * @return
     */
    @PutMapping("/news/{newsIds}")
    public boolean removeBatchByIds(@PathVariable Long[] newsIds) {
        return newsService.removeBatchByIds(Arrays.asList(newsIds));
    }

    /**
     * 修改新闻
     * @param news
     * @return
     */
    @PutMapping("/news")
    public boolean update(@RequestBody News news) {
        return newsService.updateById(news);
    }
}
